<?php

require_once 'config.php';

	$dbConn = mysql_pconnect (DBHOST,DBUSER,DBPASS) or die ('MySQL connect failed. ' . mysql_error());
	mysql_select_db(DBNAME);
	
	//Total groups
	$total_groups = mysql_query("SELECT count(*) from `group`") or die(mysql_error());
	$row_total_groups = mysql_fetch_row($total_groups);
	#echo $row_total_groups[0];
	
	switch ($argv[1]) {
	    case forum:
		exec("echo forum >> /home/rsoni/public_html/this.txt");
	        $post_tables = array('forum');
	        break;
	    case file:
		//exec("echo file >> /home/rsoni/public_html/this.txt");
	        $post_tables = array('file');
	        break;
	    case issue:
		//exec("echo issue >> /home/rsoni/public_html/this.txt");
	        $post_tables = array('issue');
	        break;
        case svn:
			//exec("echo svn >> /home/rsoni/public_html/this.txt");
	        $post_tables = array('svn');
	        break;
        case wiki:
			//exec("echo wiki >> /home/rsoni/public_html/this.txt");
	        $post_tables = array('wiki');
	        break;
        case all:
	        $post_tables = array('forum','file','issue','svn','wiki');
	        break;
	}

	//Find forum,file,issue,svn,wiki for all groups
	for($j=1;$j<=$row_total_groups[0];$j++) //Iterate for all groups
	{
		
		$id_group = mysql_query("SELECT `id`,`name` from `group` ") or die(mysql_error());
		
		$count=0;
		foreach($post_tables as $table)
		{
			#$count_posts = mysql_query("SELECT COUNT(*) FROM $table WHERE `group_id`=".$j." and date between ".$start_date." and ".strtotime('+7 days', $start_date)) or die(mysql_error());
			$count_posts = mysql_query("SELECT COUNT(*) FROM $table WHERE `group_id`=".$j) or die(mysql_error());
			$c=mysql_fetch_row($count_posts);
			$count+=$c[0];
		}
		
		$total_posts[$j]=$count;
	}
	
	$i=1;
	foreach($total_posts as $posts)
	if($total_posts[$i]<1000)
	$arr[$k++]=$i++;
	
	if(!empty($arr))
	{
	#mail('petkovic@sfsu.edu', 'Testing Triggers on hci', 'Testing triggers on hci.cs.sfsu.edu. Please ignore this email');
	mail('ravi123soni@gmail.com', 'Testing Triggers on hci', 'Testing triggers on hci.cs.sfsu.edu. Please ignore this email');
	}
	
	//print_r($total_posts);

?>